﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity.Infrastructure;
using nRoute.Samples.SiteMapWeb.Model;

namespace nRoute.Samples.SiteMapWeb.Data
{
	public class ApplicationSiteMapInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<ApplicationSiteMapContext>
	{
		protected override void Seed(ApplicationSiteMapContext context)
		{
			CreateBasicSiteMap(context);
			CreateAdvancedSiteMap(context);
		}

		private void CreateBasicSiteMap(ApplicationSiteMapContext context)
		{
			// site map
			var _applicationSiteMap = new ApplicationSiteMap() { ApplicationName = "BasicSiteMap" };
			context.SiteMaps.Add(_applicationSiteMap);

			// root node
			var _rootSiteMapNode = new ApplicationSiteMapNode() { Key = "RootNode" };
			_applicationSiteMap.RootSiteMapNode = _rootSiteMapNode;
			context.SiteMapNodes.Add(_rootSiteMapNode);

			// nodes
			_rootSiteMapNode.ChildNodes = new List<ApplicationSiteMapNode>();
			var _childNode1 = new ApplicationSiteMapNode()
			{
				Key = "Home",
				Title = "Home",
				Url = "Content/Pages/Home/",
				ListOrder = 0,
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode1);
			context.SiteMapNodes.Add(_childNode1);

			var _childNode2 = new ApplicationSiteMapNode()
			{
				Key = "About",
				Title = "About",
				Url = "Content/Pages/About/",
				ListOrder = 1
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode2);
			context.SiteMapNodes.Add(_childNode2);
		}

		private void CreateAdvancedSiteMap(ApplicationSiteMapContext context)
		{
			// site map
			var _applicationSiteMap = new ApplicationSiteMap() { ApplicationName = "AdvancedSiteMap" };
			context.SiteMaps.Add(_applicationSiteMap);

			// site area
			_applicationSiteMap.SiteMapAreas = new List<ApplicationSiteMapArea>();
			var _siteMapArea = new ApplicationSiteMapArea()
			{
				InitializeOnLoad = true,
				Key = "SiteMapModule",
				RemoteUrl = "nRoute.Samples.SiteMapModule.xap",
			};
			context.SiteMapAreas.Add(_siteMapArea);
			_applicationSiteMap.SiteMapAreas.Add(_siteMapArea);

			// root node
			var _rootSiteMapNode = new ApplicationSiteMapNode() { Key = "RootNode" };
			_applicationSiteMap.RootSiteMapNode = _rootSiteMapNode;
			context.SiteMapNodes.Add(_rootSiteMapNode);

			// nodes
			_rootSiteMapNode.ChildNodes = new List<ApplicationSiteMapNode>();
			var _childNode0 = new ApplicationSiteMapNode()
			{
				Key = "Home",
				Title = "Home",
				Url = "Content/Pages/Home/",
				ListOrder = 0,
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode0);
			context.SiteMapNodes.Add(_childNode0);

			var _childNode1 = new ApplicationSiteMapNode()
			{
				Key = "Orders",
				Title = "Orders",
				Url = "Content/Pages/Orders/",
				ListOrder = 1,
				SiteMapArea = _siteMapArea
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode1);
			context.SiteMapNodes.Add(_childNode1);

			_childNode1.ChildNodes = new List<ApplicationSiteMapNode>();
			var _childNodeA = new ApplicationSiteMapNode()
			{
				Key = "Recent",
				Title = "Recent Orders",
				Url = "Content/Pages/Orders/Recent/",
				ListOrder = 0,
				SiteMapArea = _siteMapArea
			};
			_childNode1.ChildNodes.Add(_childNodeA);
			context.SiteMapNodes.Add(_childNodeA);

			var _childNodeB = new ApplicationSiteMapNode()
			{
				Key = "Pending",
				Title = "Pending Orders",
				Url = "Content/Pages/Orders/Pending/",
				ListOrder = 1,
				SiteMapArea = _siteMapArea
			};
			_childNode1.ChildNodes.Add(_childNodeB);
			context.SiteMapNodes.Add(_childNodeB);

			var _childNodeC = new ApplicationSiteMapNode()
			{
				Key = "Completed",
				Title = "Completed Orders",
				Url = "Content/Pages/Orders/Completed/",
				ListOrder = 2,
				SiteMapArea = _siteMapArea
			};
			_childNode1.ChildNodes.Add(_childNodeC);
			context.SiteMapNodes.Add(_childNodeC);

			var _childNode2 = new ApplicationSiteMapNode()
			{
				Key = "Products",
				Title = "Products",
				Url = "Content/Pages/Products/",
				ListOrder = 2
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode2);
			context.SiteMapNodes.Add(_childNode2);

			var _childNode3 = new ApplicationSiteMapNode()
			{
				Key = "Services",
				Title = "Services",
				Url = "Content/Pages/Services/",				
				ListOrder = 3
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode3);
			context.SiteMapNodes.Add(_childNode3);

			var _childNode4 = new ApplicationSiteMapNode()
			{
				Key = "Support",
				Title = "Support",
				Url = "Content/Pages/Support/",
				ListOrder = 4
			};
			_rootSiteMapNode.ChildNodes.Add(_childNode4);
			context.SiteMapNodes.Add(_childNode4);
		}
	}
}